<div class="main-content">
    <div class="section__content section__content--p20">
        <div class="container-fluid">
            <div class="row m-b-30">
                <div class="col-lg-12">
                    <div class="overview-wrap">
                        <h2 class="title-1">栏目管理</h2>
                        <button class="au-btn au-btn-icon au-btn--blue"  onclick="addFirst()">
                            <i class="zmdi zmdi-plus"></i>添加栏目</button>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="table-responsive table--no-card m-b-30">
                        <table class="table table-borderless table-striped table-earning ">
                            <thead>
                            <tr>
                                <th>排序</th>
                                <th>栏目名称</th>
                                <th>导航栏显示</th>
                                <th>模块</th>
                                <th>ID</th>
                                <th>目录名称</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            foreach ($modules as $m) {
                                ?>
                                <tr parentid="-1"
                                    class="<?php echo $m['childcounts'] > 0 ? "haschild" : ""; ?> tr_<?php echo $m['module_id']; ?>"
                                    id="<?php echo $m['module_id']; ?>">
                                    <td><?php echo $m['seq']; ?></td>
                                    <td><?php echo $m['childcounts'] > 0 ? "—&nbsp;" : ""; ?>
                                        <?php echo $m['category']; ?>
                                    </td>
                                    <td>
                                        <?php
                                        if ($m['ishid'] == 0) {
                                            ?>
                                            是
                                            <?php
                                        } else {
                                            ?>
                                            否
                                            <?php
                                        }
                                        ?>
                                    </td>
                                    <td><?php echo $this->getModuleName($m['module']); ?></td>
                                    <td><?php echo $m['module_id']; ?></td>
                                    <td><?php echo $m['module'] == 'link' ? $m['url'] : $m['module']; ?></td>
                                    <td>
                                        <button type="button" class="btn btn-primary btn-sm"  onclick="showNewAdd(<?php echo $m['module_id']; ?>)">新建</button>
                                        <button type="button" class="btn btn-primary btn-sm" onclick="showedit(<?php echo $m['module_id']; ?>)">编辑</button>
                                        <button type="button" class="btn btn-danger btn-sm"  data-toggle="modal" data-target="#confirmModal" onclick="del(<?php echo $m['module_id']; ?>)">删除</button>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    var hasLoadAll = Array();
    var haschilditems = null;

    $(function () {
        haschilditems = $(".haschild");
        loadChildItems();
    });
    function loadChildItems() {
        if (haschilditems.length > 0) {
            for (var i = 0; i < haschilditems.length; i++) {
                $.get('index.php?r=admin/module/ajax_LoadChilds', {id: $(haschilditems[i]).attr("id")}, function (res) {
                    $(".tr_" + res.data.id).after(res.data.html);
                    hasLoadAll.push(res.data.id);
                    reloadScript();
                }, 'json');
            }
        }
    }
    function reloadScript() {
        if (hasLoadAll.length == haschilditems.length) {
            $.getScript("static/admin/theme/vendor/jquery-3.2.1.min.js");
            $.getScript("static/admin/theme/vendor/bootstrap-4.1/bootstrap.min.js");
            $.getScript("static/admin/lib/bootstrap-dialog/js/bootstrap-dialog.min.js");
        }
    }
    function triggerAfterLoad() {
        if (hasLoadAll.length == haschilditems.length) {
            $("table.module tbody tr").hover(
                function () {
                    $(this).addClass("hover");
                },
                function () {
                    $(this).removeClass("hover");
                }
            );
        }
    }
    function addSecond(moduleid) {
        var tr = $("#temp_table").find(".tr_second").clone();
        $(tr).attr("parentid", moduleid);
        $(tr).find(".module_en").html($(tr).find("select[name=module]").val());
        var cld = $("tr[parentid=" + moduleid + "]:last");
        if (cld.length == 0) {
            $("tr[id=" + moduleid + "]").after($(tr));
        } else {
            $("tr[parentid=" + moduleid + "]:last").after($(tr));
        }
    }
    function addThird(moduleid) {
        var tr = $("#temp_table").find(".tr_third").clone();
        $(tr).attr("parentid", moduleid);
        $(tr).find(".module_en").html($(tr).find("select[name=module]").val());
        var cld = $("tr[parentid=" + moduleid + "]:last");
        if (cld.length == 0) {
            $("tr[id=" + moduleid + "]").after($(tr));
        } else {
            $("tr[parentid=" + moduleid + "]:last").after($(tr));
        }
    }
    function addFirst() {
        window.location.href = "index.php?r=admin/module/editmodule&parentid=0";
    }
    function changeNextTd(obj) {
        if ($(obj).val() == 'link') {
            //类型是链接的时候
            $(obj).parent("td").next("td").html("<input type='text' class='ui' name='url'/>");
        } else {
            $(obj).parent("td").next("td").html($(obj).val());
        }

    }
    function saveModule() {
        if ($("#form_module").validationEngine('validate')) {
            mask();
            var trs = $("#module_table tbody").find("tr");
            if (trs.length > 0) {
                //JSON.stringify
                var modules = new Array();
                for (var i = 0; i < trs.length; i++) {
                    modules.push({
                        module_id: $(trs[i]).attr("id"),
                        category: $(trs[i]).find("input[name=category]").val(),
                        parent_id: $(trs[i]).attr("parentid"),
                        seq: $(trs[i]).find("input[name=seq]").val(),
                        ishid: $(trs[i]).find("select[name=ishid]").val(),
                        module: $(trs[i]).find("select[name=module]").val(),
                        url: $(trs[i]).find("input[name=url]").val()
                    });
                }
                $.post('index.php?r=admin/module/Ajax_ModuleEdit', {
                    modulestrs: JSON.stringify(modules),
                    YII_CSRF_TOKEN: $("input[name=YII_CSRF_TOKEN]").val()
                }, function (res) {
                    if (res.type == true) {
                        window.location.reload();
                    } else {
                        alert(res.message);
                    }
                    unmask();
                }, 'json');
            } else {
                alert("栏目数量不能为空");
            }
        }
    }
    function del(moduleid) {
        //删除模块
        /*confirm('确定删除栏目？删除栏目会将栏目对应的内容，全部删掉，确定删除？',function () {
            //删除栏目
            $.get('index.php?r=admin/module/del', {moduleid: moduleid}, function (result) {
                if(message(result)){
                    window.location.href = "index.php?r=admin/module/index";
                }
            }, 'json');
        });*/
        $("#confirmModal_text").text('确定删除栏目？删除栏目会将栏目对应的内容，全部删掉，确定删除？');
        $("#confirmModalButton").bind("click",function () {
            $.get('index.php?r=admin/module/del', {moduleid: moduleid}, function (result) {
                if(message(result)){
                    window.location.href = "index.php?r=admin/module/index";
                }
            }, 'json');
        });
    }
    function showedit(moduleid) {
        window.location.href = "index.php?r=admin/module/editmodule&module_id="+moduleid;
    }
    function showNewAdd(parentid) {
        window.location.href = "index.php?r=admin/module/editmodule&parentid="+parentid;
    }
    function delParentTr(obj) {
        $(obj).closest("tr").remove();
    }
</script>

